package com.example.gdyh.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.gdyh.dao.TPreGrpLinkInfoMapper;
import com.example.gdyh.pojo.Jsons;
import com.example.gdyh.pojo.TPreGrpLinkInfo;
import com.example.gdyh.util.MyException;
import java.time.LocalDate;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin
@RequestMapping({"/contacts"})
public class ContactsController {
    @Autowired
    private TPreGrpLinkInfoMapper tPreGrpLinkInfoMapper;

    @RequestMapping({"/InsertContacts"})
    public Jsons<Object> InsertContacts(@RequestBody JSONObject jsonObject) throws MyException {
        List<TPreGrpLinkInfo> list = (List<TPreGrpLinkInfo>)jsonObject.get("res");
        System.out.println(jsonObject.toJSONString());
        int i = this.tPreGrpLinkInfoMapper.batchInsert(list);
        if (i > 0) {
            Jsons<Object> jsons = new Jsons();
            jsons.setCode("200");
            jsons.setMessage("添加成功");
            jsons.setResult("+ i + ");
            return jsons;
        }
        throw new MyException("500", "添加失败");
    }

    @RequestMapping({"/UpdateContacts"})
    public Jsons<Object> UpdateContacts(@RequestBody JSONObject jsonObject) throws MyException {
        List<TPreGrpLinkInfo> list = (List<TPreGrpLinkInfo>)jsonObject.get("res");
        int i;
        for (i = 0; i < list.size(); i++) {
            ((TPreGrpLinkInfo)list.get(i)).setMakedate(LocalDate.now());
            ((TPreGrpLinkInfo)list.get(i)).setMaketime(LocalDate.now().toString());
        }
        System.out.println("fu" + list);
        i = this.tPreGrpLinkInfoMapper.updateBatch(list);
        if (i > 0) {
            Jsons<Object> jsons = new Jsons();
            jsons.setCode("200");
            jsons.setMessage("修改成功");
            jsons.setResult("+ i + ");
            return jsons;
        }
        throw new MyException("500", "修改失败");
    }
}
